package com.xx.java8.chapter3;

import com.xx.java8.domain.Apple;
import com.xx.java8.domain.Color;
import javafx.beans.value.ChangeListener;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;

public class App {
    public static void main(String[] args) {
//        processFile(new BufferedReaderProcessor() {
//            @Override
//            public String process(BufferedReader br) throws Exception {
//                return null;
//            }
//        });
//
//        processFile((BufferedReader br) -> {
//            return br.readLine();
//        });
//
//        processFile((BufferedReader br) -> br.readLine() + "dd");

        List<Integer> integerList = map(Arrays.asList("Java", "EBook", "helloworld"), (String s) -> s.length());
        System.out.println(integerList);

        List<String> names = Arrays.asList("是可惜", "多少钱", "发送", "发现看起来");
//        names.sort((str1, str2) -> str1.compareToIgnoreCase(str2));
        names.sort(String::compareToIgnoreCase);
        System.out.println(names);

        Supplier<Apple> appleSupplier = Apple::new;
        Apple apple = appleSupplier.get();
        System.out.println(apple);

        Function<Double, Apple> appleFunction = Apple::new;
        Apple apple1 = appleFunction.apply(100.0);
        System.out.println(apple1);


        TriFunction<Integer, Integer, Integer, Color> triFunction = Color::new;
        Color color = triFunction.apply(255, 255, 255);
        System.out.println(color);
    }

    static String processFile(BufferedReaderProcessor processor) {
        try (BufferedReader br = new BufferedReader(new FileReader("data.txt"))) {
            return processor.process(br);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    static <T, R> List<R> map(List<T> list, Function<T, R> function) {
        List<R> result = new ArrayList<>();
        for (T t : list) {
            result.add(function.apply(t));
        }
        return result;
    }
}
